package com.city.eduservice.controller;


import com.city.commonutils.Result;
import com.city.eduservice.entity.EduVideo;
import com.city.eduservice.feignClient.VodServiceFeignClient;
import com.city.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author city
 * @since 2020-07-01
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodServiceFeignClient vodServiceFeignClient;
    /**
     * 前端需要传后端储存视频后返回的链接回来对小节进行添加
     *
     * @param eduVideo
     * @return
     */
    @PostMapping("addVideo")
    public Result addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return Result.ok();
    }

    /**
     * 后期修改，需要接连的删除小节的数据库存储和阿里云视频存储
     * 通过springcloud来调用另一个阿里云微服务来同时删除阿里云视频
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result deleteVideo(@PathVariable String id){
        EduVideo byId = eduVideoService.getById(id);
        String videoSourceId = byId.getVideoSourceId();
        //调用feign接口调用vod模块删除阿里云视频
        if (!StringUtils.isEmpty(videoSourceId)){
            vodServiceFeignClient.removeAlyVideo(videoSourceId);
        }
        eduVideoService.removeById(id);
        return Result.ok();
    }

    /**
     * 修改
     */
}